<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                :model="queryParams"
                class="mb-[-16px]"
                inline
            >
                <el-form-item class="w-[280px]" label="分类" prop="type">
                    <el-select v-model="queryParams.type" clearable placeholder="请选择分类">
                        <el-option label="首页" value="index_banner" />
                        <el-option label="下单页" value="order_banner" />
                        <el-option label="用户焦点图" value="user_focus" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card v-loading="pager.loading" class="!border-none" shadow="never">
            <el-button v-perms="['boom_banner/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <!--            <el-button-->
            <!--                v-perms="['boom_banner/delete']"-->
            <!--                :disabled="!selectData.length"-->
            <!--                @click="handleDelete(selectData)"-->
            <!--            >-->
            <!--                删除-->
            <!--            </el-button>-->
            <div class="mt-4">
                <el-table :data="pager.lists">
                    <el-table-column label="图片" prop="image" show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-image
                                :src="row.image"
                                fit="cover"
                                style="width: 100px; height: 100px"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column label="代理商" prop="agent_id" show-overflow-tooltip>
                        <template #default="{ row }">
                            {{ agent_list.find(item => item.id === row.agent_id)?.name }}
                        </template>
                    </el-table-column>
                    <el-table-column label="排序" prop="sort" show-overflow-tooltip />
                    <el-table-column label="分类" prop="type"
                                     show-overflow-tooltip>
                        <template #default="{ row }">
                            {{ row.type === 'index' ? '下单页' : '首页' }}
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="120">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['boom_banner/edit']"
                                link
                                type="primary"
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['boom_banner/delete']"
                                link
                                type="danger"
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @close="showEdit = false" @success="getLists" />
    </div>
</template>

<script lang="ts" name="boomBannerLists" setup>
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiBoomBannerDelete, apiBoomBannerLists } from '@/api/boom_banner'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { apiBoomAgentLists } from '@/api/agent'

const agent_list = ref([])

const getGentLists = async () => {
    const data = await apiBoomAgentLists({
        page: 1,
        page_size: 10000
    })
    agent_list.value = data.lists
}

getGentLists()

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    type: ''
})


// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiBoomBannerLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiBoomBannerDelete({ id })
    getLists()
}

getLists()
</script>

